package com.hsteach.web.controller.rota;

import com.hsteach.common.core.controller.BaseController;
import com.hsteach.common.core.domain.AjaxResult;
import com.hsteach.common.core.page.TableDataInfo;
import com.hsteach.common.utils.SecurityUtils;
import com.hsteach.common.utils.poi.ExcelUtil;
import com.hsteach.dao.domain.rota.Rota;
import com.hsteach.dao.domain.rota.RotaDetail;
import com.hsteach.enums.BooleanEnum;
import com.hsteach.service.rota.RotaDetailService;
import com.hsteach.service.rota.RotaService;
import com.hsteach.web.controller.rota.dto.AddRotaDto;
import com.hsteach.web.controller.rota.dto.UpdateRotaDto;
import com.hsteach.web.dto.PageDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 值班主表Controller
 * 
 * @author fanxquer
 * @date 2023-10-18
 */
@Api(value = "值班表管理", tags = "值班表管理")
@RestController
@RequestMapping("/rota")
public class RotaController extends BaseController
{
    @Autowired
    private RotaService rotaService;

    @Autowired
    private RotaDetailService rotaDetailService;

    /**
     * 查询值班主表列表
     */
    @ApiOperation("分页查询值班列表")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody PageDto dto)
    {

        return rotaService.listRota(dto, BooleanEnum.FALSE.getCode());
    }

    /**
     * 查询值班主表列表
     */
    @ApiOperation("分页查询所有值班列表")
    @PostMapping("/listRecordAll")
    public TableDataInfo listRecordAll(@RequestBody PageDto dto)
    {
        return rotaService.listRota(dto, null);
    }

    /**
     * 查询值班主表列表 已归档
     */
    @ApiOperation("分页查询已归档值班列表")
    @PostMapping("/listFiled")
    public TableDataInfo listFiled(@RequestBody PageDto dto)
    {
        return rotaService.listFiled(dto);
    }

    /**
     * 查询所有未归档值班列表
     */
    @ApiOperation("查询所有未归档值班列表")
    @GetMapping("/listAll")
    public TableDataInfo listAll()
    {
        return rotaService.listMyRotaCache(SecurityUtils.getAuthInfo().getPersonId());
//        List<ListRotaVo> ret = tableDataInfo.getRows().stream().filter(e -> BooleanEnum.FALSE.getCode().equals(e.getFileFlg())).collect(Collectors.toList());
//        return getDataTable(ret);
    }


    /**
     * 获取值班主表详细信息
     */
    @ApiOperation("查询值班表信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(rotaService.selectRotaById(id));
    }

    /**
     * 新增值班主表
     */
    @ApiOperation("新增值班表")
    @PostMapping
    public AjaxResult add(@RequestBody AddRotaDto dto)
    {
        Rota rota = new Rota();
        BeanUtils.copyProperties(dto, rota);
        rota.setCreateBy(SecurityUtils.getAuthInfo().getPersonId());
        rota.setDelStatus(BooleanEnum.TRUE.getCode());
        rota.setFileFlg(BooleanEnum.FALSE.getCode());
        rota.setOrgCode(SecurityUtils.getAuthInfo().getOrgCode());
        return toAjax(rotaService.insertRota(rota));
    }

    /**
     * 修改值班主表
     */
    @ApiOperation("修改值班表")
    @PutMapping
    public AjaxResult edit(@RequestBody UpdateRotaDto dto)
    {
        Rota rota = new Rota();
        BeanUtils.copyProperties(dto, rota);
        rota.setUpdateBy(SecurityUtils.getAuthInfo().getPersonId());
        return toAjax(rotaService.updateRota(rota));
    }

    /**
     * 删除值班主表
     */
    @ApiOperation("删除值班表")
    @DeleteMapping("/{id}")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult remove(@PathVariable Long id)
    {
        Rota update = new Rota();
        update.setId(id);
        update.setDelStatus(BooleanEnum.FALSE.getCode());
        update.setUpdateBy(SecurityUtils.getAuthInfo().getPersonId());

        // 删除明细表
        RotaDetail rotaDetail = new RotaDetail();
        rotaDetail.setRotaId(id);
        rotaDetail.setDelStatus(BooleanEnum.FALSE.getCode());
        rotaDetailService.updateByRotaId(rotaDetail);
        return toAjax(rotaService.updateRota(update));
    }

    /**
     * 归档值班表
     */
    @ApiOperation("归档值班表")
    @PostMapping("/file/{id}")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult file(@PathVariable Long id)
    {
        Rota update = new Rota();
        update.setId(id);
        update.setFileFlg(BooleanEnum.TRUE.getCode());
        return toAjax(rotaService.updateRota(update));
    }

    /**
     * 取消归档值班表
     */
    @ApiOperation("取消归档值班表")
    @PostMapping("/unfile/{id}")
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult unfile(@PathVariable Long id)
    {
        Rota update = new Rota();
        update.setId(id);
        update.setFileFlg(BooleanEnum.FALSE.getCode());
        return toAjax(rotaService.updateRota(update));
    }
}
